<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/22 11:24
 * Desc: 产品地址管理
 */
class Controller_Pc_Api_Standard_Address extends Controller_Pc_Api_Base
{

    private $_mid;

    public function before()
    {
        parent::before();
        // 检查用户是否有效
        $mid = intval($this->request_body->content->mid);
        $check_member = Model_Api_Standard_Pub::check_member_effective($mid);
        if($check_member['status'] == false)
        {
            $this->send_datagrams($this->client_info['id'], $check_member, $this->client_info['secret_key']);
        }

        $this->_mid = $mid;
    }

    /**
     * @function 获取个人地址列表
     */
    public function action_address_list()
    {
        $page = intval($this->request_body->content->page);
        $pagesize = intval($this->request_body->content->pagesize);
        $page = $page < 1 ? 1 : $page;
        $page_size = $pagesize ? $pagesize : 10;
        $list = DB::select()->from('member_address')->where('memberid', '=',$this->_mid)->order_by('is_default','desc')->order_by('id','desc')->limit($page_size)->offset(($page - 1) * $page_size)->execute()->as_array();
        $this->send_datagrams($this->client_info['id'], array('list'=>$list,'is_finished'=>true), $this->client_info['secret_key']);
    }

    /**
     * @function 删除个人地址
     */
    public function action_address_del()
    {
        $id = intval($this->request_body->content->id);
        if(!$id)
        {
            $result = array('status'=> false, 'msg'=> '提交信息有误');
        }
        else
        {
            $del = DB::delete('member_address')->where('memberid', '=',$this->_mid)->and_where('id','=',$id)->execute();
            $result = $del? array('status'=>true,'msg'=> '删除成功') : array('status'=>false,'msg'=> '删除失败');
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取地址信息
     */
    public function action_address_info()
    {
        $addressid = intval($this->request_body->content->addressid);
        if(!$addressid)
        {
            $result = array('status'=> false, 'msg'=> '提交信息有误');
        }
        else
        {
            $info = DB::select()->from('member_address')->where('memberid', '=',$this->_mid)->and_where('id','=',$addressid)->execute()->current();
            if($info)
            {
                $result = array('status'=> true,'msg'=> '获取成功','info'=> $info);
            }
            else
            {
                $result = array('status'=> false,'msg'=> '查询地址信息失败');
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 保存地址信息
     */
    public function action_address_save()
    {
        $addressid = intval($this->request_body->content->addressid);
        $city    =  $this->request_body->content->city;
        $address =  $this->request_body->content->address;
        $phone   =  $this->request_body->content->phone;
        $receiver =  $this->request_body->content->receiver;
        $postcode =  $this->request_body->content->postcode;
        $province =  $this->request_body->content->province;
        $is_default =  $this->request_body->content->is_default;

        $model = ORM::factory('member_address')->where('memberid','=',$this->_mid)->and_where('id','=',$addressid)->find();
        $model->memberid = $this->_mid;
        $model->province = $province;
        $model->city = $city;
        $model->address = $address;
        $model->phone = $phone;
        $model->receiver = $receiver;
        $model->postcode = $postcode;
        $model->is_default = $is_default;
        $model->save();
        if($model->saved())
        {
            if($model->is_default==1)
            {
                DB::update('member_address')->set(array('is_default'=>0))->where('memberid','=',$this->_mid)->and_where('id','!=',$model->id)->execute();
            }
            $this->send_datagrams($this->client_info['id'], array('status'=>true), $this->client_info['secret_key']);
        }
        else
        {
            $this->send_datagrams($this->client_info['id'], array('status'=>false,'msg'=>'保存失败'), $this->client_info['secret_key']);
        }
    }

    /**
     * @function 获取快递所有的城市
     */
    public function action_address_city()
    {
        $list = DB::select()->from('express_destination')->where('pid','=',0)->execute()->as_array();
        foreach($list as &$v)
        {
            $v['children'] = DB::select()->from('express_destination')->where('pid','=',$v['id'])->execute()->as_array();
        }
        $this->send_datagrams($this->client_info['id'], $list, $this->client_info['secret_key']);
    }
}